<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="证书名称" prop="certName">
        <el-input
          v-model="queryParams.certName"
          placeholder="请输入证书名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="证书类型" prop="certValue">
        <el-select
          v-model="queryParams.certValue"
          placeholder="请选择证书类型"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in dict.type.sys_cert_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['company:certificate:add']"
          >创建</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table fit v-loading="loading" border :data="dataList">
      <el-table-column
        label="证书名称"
        prop="certName"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column label="证书编号/专利号" width="200" prop="num" />

      <el-table-column label="证书类型" align="center" width="100">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_cert_type"
            :value="scope.row.certValue"
          />
        </template>
      </el-table-column>
      <el-table-column width="100" label="颁发部门" prop="dept">
      </el-table-column>
      <el-table-column width="100" label="有效日期" prop="limitDate">
        <template slot-scope="scope">
          {{ scope.row.limitDate
          }}<span v-if="scope.row.remainDays"
            >({{ scope.row.remainDays }})</span
          >
        </template>
      </el-table-column>
      <el-table-column
        label="发证日期/公告时间"
        prop="publishDate"
        width="220"
      />
      <el-table-column label="发明人" width="100" prop="userName" />

      <el-table-column
        label="经办人"
        :show-overflow-tooltip="true"
        prop="operUserName"
        width="100"
      />
      <el-table-column label="记录" prop="createTime" />

      <el-table-column label="公司基本资料" align="center" width="100">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isCompBase"
            active-value="1"
            inactive-value="0"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="200"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            v-hasPermi="['company:certificate:info']"
            @click="handleDetail(scope.row)"
            >详情</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['company:certificate:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['company:certificate:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改公司公告对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form
        ref="elForm"
        :model="formData"
        :rules="rules"
        :disabled="disabled"
        size="medium"
        label-width="150px"
      >
        <el-form-item label="证书类型" prop="certValue">
          <el-select
            v-model="formData.certValue"
            placeholder="请选择证书类型"
            clearable
            :style="{ width: '100%' }"
          >
            <el-option
              v-for="dict in dict.type.sys_cert_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="提交申请日"
          prop="applyDate"
          v-if="formData.certValue == 2"
        >
          <el-date-picker
            v-model="formData.applyDate"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            :style="{ width: '100%' }"
            placeholder="请选择提交申请日"
            clearable
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="证书名称" prop="certName">
          <el-input
            v-model="formData.certName"
            placeholder="请输入证书名称"
            clearable
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
        <el-form-item label="证书编号/专利号" prop="num">
          <el-input
            :disabled="title !== '创建证书'"
            v-model="formData.num"
            placeholder="请输入证书编号/专利号"
            clearable
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
        <el-form-item label="证书/专利" prop="certImage">
          <imageUpload
            :disabled="title !== '创建证书'"
            :fileType="['png', 'jpg', 'jpeg', 'PDF', 'pdf']"
            v-model="formData.certImage"
          />
        </el-form-item>
        <el-form-item label="颁发部门" prop="dept">
          <el-input
            v-model="formData.dept"
            placeholder="请输入颁发部门"
            clearable
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
        <el-form-item label="发证日期/ 公告时间" prop="publishDate">
          <el-date-picker
            v-model="formData.publishDate"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            :style="{ width: '100%' }"
            placeholder="请选择发证日期/ 公告时间"
            clearable
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="有效期" prop="limitDate">
          <el-date-picker
            v-model="formData.limitDate"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            :style="{ width: '100%' }"
            placeholder="请选择有效期"
            clearable
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="">
          <el-checkbox
            v-model="formData.isRemind"
            true-label="1"
            false-label="0"
            >设置到期提醒时间</el-checkbox
          >
          <el-select
            v-model="formData.remindType"
            placeholder="请选择提醒时间"
            :style="{ width: '100%' }"
          >
            <el-option label="1个月" value="1" />
            <el-option label="2个月" value="2" />
            <el-option label="3个月" value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="发明人" prop="userName">
          <el-input
            v-model="formData.userName"
            placeholder="请输入发明人"
            clearable
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
        <el-form-item label="经办人" prop="operUserName">
          <el-input
            v-model="formData.operUserName"
            placeholder="请输入经办人"
            clearable
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  certList,
  certAdd,
  certEdit,
  certDel,
  statusChange,
} from "@/api/company/cert.js";
export default {
  name: "company",
  dicts: ["sys_cert_type"],
  data() {
    return {
      isRemind: false,
      dataList: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      form: {
        status: 0,
        noticeType: "1",
      },
      // 表单校验
      formData: {
        certValue: undefined,
        certName: undefined,
        num: undefined,
        dept: undefined,
        publishDate: null,
        limitDate: null,
        applyDate: null,
        userName: undefined,
        operUserName: undefined,
        remindType: "1",
        isRemind: "0",
      },
      rules: {
        certImage: [
          {
            required: true,
            message: "图片不能为空",
            trigger: "change",
          },
        ],
        certValue: [
          {
            required: true,
            message: "请选择证书类型",
            trigger: "change",
          },
        ],
        certName: [
          {
            required: true,
            message: "请输入证书名称",
            trigger: "blur",
          },
        ],
        num: [
          {
            required: true,
            message: "请输入证书编号/专利号",
            trigger: "blur",
          },
        ],
        dept: [
          {
            required: true,
            message: "请输入颁发部门",
            trigger: "blur",
          },
        ],
        publishDate: [
          {
            required: true,
            message: "请选择发证日期/ 公告时间",
            trigger: "change",
          },
        ],
        limitDate: [
          {
            required: false,
            message: "请选择有效期",
            trigger: "change",
          },
        ],
        applyDate: [
          {
            required: true,
            message: "请选择申请日",
            trigger: "change",
          },
        ],
        userName: [
          {
            required: false,
            message: "请输入发明人",
            trigger: "change",
          },
        ],
        operUserName: [
          {
            required: false,
            message: "请输入经办人",
            trigger: "change",
          },
        ],
      },
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        noticeTitle: undefined,
      },
      rowDetail: {},
      detailOPen: false,
      title: "创建证书",
      disabled: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleStatusChange(row) {
      let text = row.isCompBase === "1" ? "设置" : "取消设置";
      this.$modal
        .confirm('确认要"' + text + '""' + row.certName + '"证书吗？')
        .then(function () {
          return statusChange(row.certId, row.isCompBase);
        })
        .then(() => {
          this.$modal.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.isCompBase = row.isCompBase === "0" ? "1" : "0";
        });
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case "handleDelete":
          this.handleDelete(row);
          break;
        case "handleAuthUser":
          this.handleAuthUser(row);
          break;
        default:
          break;
      }
    },

    /** 提交按钮 */
    submitForm: function () {
      if (this.disabled) {
        this.open = false;
        return;
      }
      this.$refs["elForm"].validate((valid) => {
        if (valid) {
          let query = JSON.parse(JSON.stringify(this.formData));
          if (!this.formData.isRemind) {
            query.remindType = "";
          } else {
            if (!query.remindType) {
              this.$modal.msgWarning("请设置提醒时间");
            }
          }
          query.certLabel = this.dict.type.sys_cert_type.find(
            (e) => e.value === query.certValue
          ).label;
          if (this.formData.certId != undefined) {
            certEdit(query).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            certAdd(query).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleDetail(row) {
      this.open = true;
      this.disabled = true;
      this.title = "查看证书";
      this.formData = JSON.parse(JSON.stringify(row));
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.disabled = false;

      this.resetForm("elForm");
      this.formData = JSON.parse(JSON.stringify(row));
      this.open = true;
      this.title = "修改证书";
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除名称为"' + row.certName + '"的数据项？')
        .then(function () {
          return certDel(row.certId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    handleAdd() {
      this.disabled = false;

      this.resetForm("elForm");
      this.open = true;
      this.title = "创建证书";
    },
    cancel() {
      this.open = false;
      this.detailOPen = false;
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      certList(this.queryParams).then((response) => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>
